package com.ysx.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ysx.server.mapper.*;
import com.ysx.server.pojo.dto.GoodsAddDTO;
import com.ysx.server.pojo.dto.GoodsDTO;
import com.ysx.server.pojo.dto.GoodsPageQueryDTO;
import com.ysx.server.pojo.entity.*;
import com.ysx.server.pojo.vo.GoodsVO;
import com.ysx.server.recommend.service.RecommendService;
import com.ysx.server.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.ysx.server.common.UpdateContent.GOODSNAME_UPDATE;
import static com.ysx.server.common.content.*;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 商品查询 分页 名称 状态
     *
     * @param goodsPageQueryDTO
     * @return
     */
    @Override
    public Page<Goods> page(GoodsPageQueryDTO goodsPageQueryDTO) {
        PageHelper.startPage(goodsPageQueryDTO.getCurrent(),goodsPageQueryDTO.getPageSize());
        Page<Goods> page=goodsMapper.page(goodsPageQueryDTO);
        return page;
    }

    /**
     * 保存商品信息
     *
     * @param goods
     * @return
     */
    @Override
    public boolean save(Goods goods) {
        if(goods==null){
            return false;
        }
        return goodsMapper.save(goods);
    }

    /**
     * 更新商品信息
     *
     * @param goodsDTO
     * @return
     */
    @Override
    @Transactional
    public boolean update(GoodsDTO goodsDTO) {
        if(goodsDTO==null){
            return false;
        }
        //1. 判断商品名称是否修改
        if(goodsDTO.getUpdateEnum().equals(GOODSNAME_UPDATE)){
            //若是修改则修改对应评论信息
            List<Comment> commentList=commentMapper.getByGoodsId(goodsDTO.getId());
            if(commentList!=null && commentList.size()>0){
                commentList.forEach(comment -> {
                    comment.setGoodsName(goodsDTO.getGoodsName());
                    commentMapper.update(comment);
                });
            }
            //若是修改则修改对应举报信息
            List<Report> reportList=reportMapper.getByGoodsId(goodsDTO.getId());
            if(reportList!=null&&reportList.size()>0){
                reportList.forEach(report -> {
                    report.setGoodsName(goodsDTO.getGoodsName());
                    reportMapper.update(report);
                });
            }
        }
        return goodsMapper.update(goodsDTO);
    }

    /**
     * 根据id删除商品信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        if(id==null){
            return false;
        }
        return goodsMapper.delete(id);
    }

    /**
     * 通过用户id获得用户发布商品
     *
     * @param userId
     */
    @Override
    public List<Goods> getByUserId(Long userId) {
        return goodsMapper.getByUserId(userId);
    }

    /**
     * 全部商品
     * @return
     */
    @Override
    public List<Goods> getGoods() {
        return goodsMapper.getAll();
    }

    /**
     * 最新
     * @return
     */
    @Override
    public List<Goods> getNewGoods() {
        return goodsMapper.getByTime();
    }

    /**
     * 通过商品名搜索
     *
     * @param name
     * @return
     */
    @Override
    public List<Goods> getByName(String name) {
        return goodsMapper.getByName(name);
    }

    /**
     * 通过商品id查找商品
     *
     * @param id
     * @return
     */
    @Override
    public GoodsVO getById(Long id) {
        if(id<0)
            return null;
        Goods goods = goodsMapper.getById(id);
        List<Comment> comments = commentMapper.getByGoodsId(id);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
        goodsVO.setComments(comments);
        return goodsVO;
    }

    /**
     * 添加商品及商品类型
     *
     * @param goodsAddDTO
     * @return
     */
    @Override
    public boolean addGoodsAndCategory(GoodsAddDTO goodsAddDTO) {
        User user=userMapper.getByAccount(goodsAddDTO.getUserAccount());
        String image="http://localhost:8080/api/"+goodsAddDTO.getGoodsImage();
        Goods goods = new Goods();
        goods.setGoodsName(goodsAddDTO.getGoodsName());
        goods.setGoodsImage(image);
        goods.setPrice(goodsAddDTO.getPrice());
        goods.setGoodsDescription(goodsAddDTO.getGoodsDescription());
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        //新增商品类型
        if(goodsAddDTO.getTypeId() == null && !goodsAddDTO.getAddTypeName().equals("") && goodsAddDTO.getAddTypeName()!=null){
            Category category=new Category();
            category.setCategoryName(goodsAddDTO.getAddTypeName());
            category.setCategoryStatus(1);
            category.setCreateTime(LocalDateTime.now());
            category.setUpdateTime(LocalDateTime.now());
            categoryMapper.save(category);
            goods.setTypeId(category.getId());
            goods.setTypeName(category.getCategoryName());
            goods.setGoodsStatus(1);
            goods.setUserId(user.getId());
            goods.setUsername(user.getUsername());
            return goodsMapper.save(goods);
        }else{ //商品类型存在
            Category category=categoryMapper.getCategoryById(goodsAddDTO.getTypeId());
            goods.setGoodsImage(image);
            goods.setTypeId(category.getId());
            goods.setTypeName(category.getCategoryName());
            goods.setGoodsStatus(1);
            goods.setUserId(user.getId());
            goods.setUsername(user.getUsername());
            return goodsMapper.save(goods);
        }
    }
}
